// 管理用户表
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const adminUser = require('../models/model/adminUser');
const User = require('../models/model/user');
const Folder = require('../models/model/folder');
const Files = require('../models/model/file');
const TaskList = require('../models/model/taskList');

// 笔记管理后台的home页和user页的api写在同一个文件

/**
 * 辅助添加用户,判断用户是否已添加
 * @param {*} id 
 * @param {*} userId 
 */
let adminUserExist = async (id = '', userId = '') => {
    const ins = await adminUser.findAll({
        where: {
            selfId: id,
            userId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}
/**
 * 添加被管理用户
 * @param {被管理用户的id} id 
 * @param {被管理用户的用户名} username 
 * @param {管理员的id} userId 
 */
exports.addAdminUser = async (id = '', username = '', userId = '') => {
    const result = await adminUserExist(id, userId);
    if (result.length > 0) {
        return 'existed'
    }
    const ins = await adminUser.create({
        selfId: id,
        username,
        userId
    })
    if (ins) {
        return ins.toJSON();
    }
    return null;
}




/**
 * 获取管理员管理的用户
 * @param {*} id  管理员的id
 */
exports.findAdminUsers = async (id = "") => {
    const ins = await adminUser.findAll({
        where: {
            userId: id
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}
// 模糊查询管理员管理的用户,通过username查询
exports.vagueFindAdminUsers = async (userId = '', username = '') => {
    const ins = await adminUser.findAll({
        where: {
            userId,
            username: {
                [Op.like]: '%' + username + '%',
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}



// 删除单个被管理用户
exports.deleteAdminUser = async (id = "") => {
    const ins = await adminUser.destroy({
        where: {
            id
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}
// 删除多个被管理用户,传入一个数组id
exports.deleteManyAdminUser = async (idArr = '') => {
    idArr = JSON.parse(idArr);
    const ins = await adminUser.destroy({
        where: {
            id: {
                [Op.or]: idArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}


// 模糊查询,通过username模糊查询,需要添加的用户
exports.vagueFindAllAdminusers = async (username = '') => {
    const ins = await User.findAll({
        where: {
            isAdministrator: 0,
            username: {
                [Op.like]: '%' + username + '%',
            }
        }
    });
    if (ins) {
        return ins;
    }
    return null;
}

// 获取被管理用户下的所有文件夹, idArr 为被管理用户所有的id
exports.getAllFolder = async (idArr = []) => {
    const ins = await Folder.findAll({
        where: {
            userId: {
                [Op.or]: idArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

// 获取被管理用户下的所有文件,通过文件夹id获取
exports.getAllFiles = async (foldersIdArr = []) => {
    const ins = await Files.findAll({
        where: {
            folderId: {
                [Op.or]: foldersIdArr,
            }
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

// 通过被管理用户的id查询今日任务
exports.HomeTaskToday = async (idArr = []) => {
    const ins = await TaskList.findAll({
        where: {
            userId: {
                [Op.or]: idArr,
            },
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

